package com.example.administrator.im.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.example.administrator.im.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wp
 * 时间：2016/4/19 11:39
 * 描述：TODO
 */
public class AddFriendsActivity extends Activity {
    @Bind(R.id.tv_title_middle)
    TextView tvTitleMiddle;
    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.tv_title_right)
    TextView tvTitleRight;
    @Bind(R.id.username_add)
    TextView usernameAdd;
    @Bind(R.id.ll_add_friends)
    LinearLayout llAddFriends;
    @Bind(R.id.add_friends)
    Button addFriends;

    private Map<String, Map<String, Object>> datas;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friends);
        ButterKnife.bind(this);
        tvTitleMiddle.setText("添加好友");
        tvTitleRight.setText("查找");
        tvTitleRight.setVisibility(View.VISIBLE);
        datas = new HashMap<>();
    }

    @OnClick({R.id.iv_title_left, R.id.tv_title_right, R.id.add_friends})
    public void onClick(View view) {
        String username = etUsername.getText().toString().trim();
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_title_right:
                //查找
                if (!TextUtils.isEmpty(username)) {
                    llAddFriends.setVisibility(View.VISIBLE);
                    usernameAdd.setText(username);
                }
                break;
            case R.id.add_friends:
                dialog = ProgressDialog.show(this, null, "玩命添加中", false, false);
                //添加
                addFriends(username);
                break;
        }
    }

    private void addFriends(final String username) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //参数为要添加的好友的username和添加理由
                try {
                    EMContactManager.getInstance().addContact(username, "骚年，加一个");//需异步处理
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    addFriends.setText("已发送");
                                    addFriends.setEnabled(false);
                                    dialog.dismiss();
                                    Toast.makeText(getApplicationContext(), "发送请求成功，等待对方验证", Toast.LENGTH_SHORT).show();
                                }
                            }, 2000);
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            Toast.makeText(getApplicationContext(), "添加失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }
}
